﻿using NUnit.Framework;
using XmlUnit;
using System.Linq;
using System.Xml;
using System;

namespace LinqtoCRM.Tests
{
    public class LinqtoCRMTests
    {
        protected CrmQueryProvider provider { get; private set; }

        [TestFixtureSetUp]
        public void Setup()
        {
            provider = new CrmQueryProvider(new DummyService());
        }

        private static readonly DiffConfiguration config = new DiffConfiguration(System.Xml.WhitespaceHandling.Significant);

        protected void AssertXmlEquals(string expectedXml, IQueryable q)
        {
            AssertXmlEquals(expectedXml, provider.GetQueryText(q.Expression));
        }

        private void print(string xml)
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(xml);
            doc.Save(Console.Error);
        }

        protected void AssertXmlEquals(string expectedXml, string xml)
        {
            try
            {
                XmlAssertion.AssertXmlEquals(new XmlDiff(new XmlInput(expectedXml), new XmlInput(xml), config));
            }
            catch (AssertionException e)
            {
                Console.Error.WriteLine("Expected:");
                print(expectedXml);
                Console.Error.WriteLine("\nGot:");
                print(xml);
                throw e;
            }
        }
    }
}
